<?php
/**
 * Created by PhpStorm.
 * User: tangchao
 * Date: 19-3-15
 * Time: 下午3:01
 */

namespace App\HttpController\Api;


use App\Lib\ClassArr;
use EasySwoole\Core\Component\Logger;

class Upload extends Base
{
    public function file()
    {
        try{

            $request = $this->request();
            $files = $request->getUploadedFiles();
            $files_keys = array_keys($files);
            $type = $files_keys[0];
            Logger::getInstance()->log($type.':upload');
            $classObj = new ClassArr();
            $classStats = $classObj->uploadClassStart();
            $res = ($classObj->initClass($type,$classStats,[$request,$type]))->upload();
        }catch (\Exception $e){
            Logger::getInstance()->log($type.':upload:'.$e->getMessage());
            return $this->writeJson(500,'',$e->getMessage());
        }

        if ($res){
            return $this->writeJson(200,[
                'url' => $res
            ],'OK');
        }else{
            return $this->writeJson(500,'','上传失败');
        }
    }
}